/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tallerfinal.util.distributions;

import tallerfinal.util.distributions.factory.GeneratorsFactory;
import tallerfinal.util.enums.DistributionsName;

/**
 *
 * @author ricardo
 */
public class TriangularGenerator extends Generator<Double> {
static {
        GeneratorsFactory.getInstance().registerProduct(DistributionsName.TRIANGULAR, TriangularGenerator.class);
    }
    private double a;
    private double b;
    private double c;

    public TriangularGenerator(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public Double generate() {
        double u = generator.nextDouble();
        double x ;
        if (u < (b - a) / (c - a)) {
            x = a + Math.sqrt(u * (c - a) * (b - a));
        } else {
            x = c - Math.sqrt((1.0 - u) * (c - a) * (c - b));
        }
        return x;
    }
}
